//
//  Weapon.m
//  my-BTB
//
//  Created by QuickSoluion on 9/12/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Weapon.h"
#import "GameConfig.h"
#import "BTBworld.h"
@implementation Weapon
+(id) bombWithGame:(BTBworld*)world
{
	return [[[self alloc] initWithGame:world] autorelease];
}

+(id) bombWithGame:(BTBworld*)world shape:(cpShape*)shape
{
	return [[[self alloc] initWithGame:world shape:shape] autorelease];	
}

-(id) initWithGame:(BTBworld*)world
{
	cpShape *shape = [world._SMG addCircleAt:cpvzero mass:STATIC_MASS radius:0.2];//7
	return [self initWithGame:world shape:shape];
}

-(id) initWithGame:(BTBworld*)world shape:(cpShape*)shape
{
	[super initWithShape:shape file:@"minnocham1.png"];
	
	_world = world;
	_countDown = NO;
	
	//Free the shape when we are released
	self.spaceManager = world._SMG;
	self.autoFreeShape = YES;
	
	//Handle collisions
	shape->collision_type = kBomColition;
	
	return self;
}

-(void) startCountDown:(SpaceManager *)_space:(cpBody *)_body
{
	tempSpace=_space;
	tempBody=_body;
	//Only start it if we haven't yet
	if (!_countDown)
	{		
		
		
		_countDown = YES;
		
		id f1 = [CCFadeTo actionWithDuration:.25 opacity:200];
		id f2 = [CCFadeIn actionWithDuration:.25];
		
		id d = [CCDelayTime actionWithDuration:3];
		id c = [CCCallFunc actionWithTarget:self selector:@selector(blowup)];
		
		[self runAction:[CCRepeatForever actionWithAction:[CCSequence actions:f1,f2,nil]]];
		[self runAction:[CCSequence actions:d,c,nil]];
	}
}

-(void) blowup
{
	if(tempBody!=nil&&tempBody!=nil)
	{	
		[tempSpace removeAndFreeConstraintsOnBody:tempBody];
	}
	[self.spaceManager applyLinearExplosionAt:self.position radius:150 maxForce:900000]; /// max force=4500////10000
	
	CCParticleSun *explosion = [[[CCParticleSun alloc] initWithTotalParticles:100] autorelease];
	explosion.texture= [[CCTextureCache sharedTextureCache] addImage: @"stars.png"];
	
	explosion.position = self.position;
	explosion.duration = .5;
	explosion.speed = 40;
	explosion.autoRemoveOnFinish = YES;
	explosion.blendAdditive = YES;//NO
	[_world addChild:explosion];
	[_world removeChild:self cleanup:YES];
	
	tempBody=nil;
	tempBody=nil;
}
@end
